package com.koron.css2.clientCenter.impl;

import com.koron.css2.ImportExcelInterface;
import com.koron.css2.clientCenter.bean.UserChangePersonBean;
import com.koron.css2.common.dto.HeaderInfoDto;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserPersonImport implements ImportExcelInterface{

	@Override
	public MessageBean<?> importExel(SessionFactory factory, UserInfoBean userInfo, RequestBean req,
			List<Map<String, String>> data, MultipartFile file) {
		MessageBean<?> msg = MaptoObjct(factory, userInfo,data);
		
		return msg;
	}

	@Override
	public List<HeaderInfoDto> getHeaderInfo(UserInfoBean userInfo) {
		// TODO Auto-generated method stub
		List<HeaderInfoDto> headerInfos = new ArrayList<HeaderInfoDto>();
		headerInfos.add(new HeaderInfoDto("用户姓名", true, "name"));
		headerInfos.add(new HeaderInfoDto("证件类型", true, "certType"));
		headerInfos.add(new HeaderInfoDto("证件号码", true, "certNo"));
		headerInfos.add(new HeaderInfoDto("联系方式", true, "linkTel"));
		headerInfos.add(new HeaderInfoDto("备注", true, "comments"));
		return headerInfos;
	}

	@Override
	public boolean isCheckHeader() {
		// TODO Auto-generated method stub
		return true;
	}
	/**
	 * 接口不存储数据。直接返回前端页面展示
	 * @param factory
	 * @param userInfoBean
	 * @param data
	 * @return
	 */
	
	public  MessageBean<?> MaptoObjct(SessionFactory factory,UserInfoBean userInfoBean,List<Map<String, String>> data) {
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", List.class);
		List<UserChangePersonBean> list = new ArrayList<UserChangePersonBean>();

		Map<String, String> IDT = Tools.mapDicByCode(factory, "IDT");//证件类型
		for (Map<String, String> vo: data) {
			UserChangePersonBean bean = new UserChangePersonBean();
			bean.setName(vo.get("name"));
			String certType = getKey(IDT, vo.get("certType"));
			if(certType!=null) {
				bean.setCertType(certType);
			}else {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "导入失败：证件类型"+vo.get("certType")+"，不符合要求", void.class);
			}
			bean.setCertNo(vo.get("certNo"));
			bean.setLinkTel(vo.get("linkTel"));
			bean.setComments(vo.get("comments"));
			list.add(bean);
		}
		info.setData(list);
		return info;
	}
	public static String getKey(Map<String,String> map, String value){
		for (Map.Entry<String, String> entry : map.entrySet()) {
			if(value!=null && value.equals(entry.getValue())) {
				return entry.getKey();
			}
		}
		return null;
		
	}
}
